<?php

	class Backend_AccountController extends Zend_Controller_Action {
	
		public function init() {
			
			$am = new Model_Authentication();
			
			$this->view->action_name = $this->getRequest()->getActionName();
			$this->view->controller_name = $this->getRequest()->getControllername();
		
			// Logged
			if($am->isLoggued()){
				$user = $am->getlogguedInformations();
				
				$this->view->is_logged = true;
				$this->view->user = $user;
				
			// Not logged
			} else {
				$redirector = $this->_helper->getHelper('Redirector');
				$redirector->gotoRoute(array('controller' => 'index', 'action' => 'index'));
			}
			
			$this->view->headLink()
					->appendStylesheet($this->_request->getBaseUrl().'/styles/commun.css')
					->appendStylesheet($this->_request->getBaseUrl().'/styles/template.css')
					->appendStylesheet($this->_request->getBaseUrl().'/styles/backend/account.css');
			
			// Ajax's call
		    $this->_helper->AjaxContext()
		         ->addActionContext('prc-signin', 'json')
		         ->initContext();
		    
		    $this->view->baseUrl = $this->_request->getBaseUrl();
			
			// change layout
            $layout = Zend_Layout::getMvcInstance();
			$layout->setLayout('layout_admin');
		}
		
		
		public function setAction() {
			
			$this->view->headTitle('Hybrid Legacy - Administration - Gérer mon compte');
					
			$this->view->headLink()
					->appendStylesheet($this->_request->getBaseUrl().'/styles/backend/form.css');
					
			$this->view->jQuery()
					->addJavascriptFile($this->view->baseUrl('/javascript/jquery/jquery.js'))
					->addJavascriptFile($this->view->baseUrl('/javascript/jquery/jquery.form.js'))
					->addJavascriptFile($this->view->baseUrl('/javascript/jquery/jquery.validation.js'))
					->addJavascriptFile($this->view->baseUrl('/javascript/jquery/jquery.validation.rules_custom.js'))
					->addJavascriptFile($this->view->baseUrl('/javascript/jquery/jquery.validation.messages_fr.js'))
					->addJavascriptFile($this->view->baseUrl('/javascript/backend/account-set.js'));
			
			// get user's informations
			$uM = new Model_User();
			$filter = array('id' => $this->view->user['id']);
			$user = $uM->get($filter);
			
			// change birth format
			$date_temp = explode(' ', $user[0]['birth']);
			$date_tab = explode('-', $date_temp[0]);
			$date = $date_tab[2].'/'.$date_tab[1].'/'.$date_tab[0];
			$user[0]['birth'] = $date;
			
			$this->view->datas = $user[0];
			
		}
		
		public function prcSetAction() {
		
			$datas = $this->_request->getParams();
			
			// check
			$errors = array();
			
			/*
			 * REQUIRED FIELDS
			 */
			
			// password_verif
			if(is_null($datas['password_verif']) or empty($datas['password_verif'])) {
				$errors[] = 'Mot de passe';
			}
			
			
			// email
			if(!is_null($datas['email']) and !empty($datas['email'])) {
				
				// check if is email
				$pattern='#^[\w.-]+@[\w.-]+\.[a-zA-Z]{2,6}$#'; 
				if(!preg_match($pattern, $datas['email'])) {
					$errors[] = 'Adresse email';
				}
				
				// check if exist
				$udM = new Model_User();
					
				// check if is used
				$filter = array('email' => $datas['email']);
				$exist = $udM->get($filter);
				// if exist and is not user's nickname
				if(isset($exist[0]) and $datas['email'] != $this->view->user['email']) {
					$errors[] = 'Email (Déjà utilisé, veuillez en sélectionner un autre)';
				}
			}
			
			/*
			 * OTHERS FIELDS
			 */
			
			
			// password
			if(!is_null($datas['password']) and !empty($datas['password'])) {
				
				// length
				if(strlen($datas['password']) < 6) {
					echo 'toto';
					$errors[] = 'Nouveau mot de passe (trop court)';
				}
				
				if(is_null($datas['password_confirm'] or empty($datas['password_confirm']))) {
					$errors[] = 'Confirmation du mot de passe';
				
				} else {
					if($datas['password'] != $datas['password_confirm']) {
						$errors[] = 'Le mot de passe et sa confirmation douvent être identiques';
					
					} else {
						$password_modifyed = true;
					}
				}
			}
			
			
			// if errors
			if(!empty($errors)) {
				$message = 'Veuillez vérifier les champs suivant :';
				$message .= '<ul>';
				foreach($errors as $e) {
					$message .= '<li>'.$e.'</li>';
				}
				$message .= '</ul>';
				
				exit(Zend_Json::encode(array('result' => false, 'message' => $message)));
			}
			
			$uM = new Model_User();
			
			// check email / password couple
			$aM = new Model_Authentication();
			$filter = array('email' => $datas['email'],
							'password' => $aM->encrypt($datas['password_verif'])
							);
			$user = $uM->get($filter);
			if(is_null($user) or empty($user)) {
				exit(Zend_Json::encode(array('result' => false, 'message' => 'Mot de passe incorrect')));
			}
			
			// set user
			$user = array(	'email' => $datas['email'],
							'update' => date('Y-m-d H:i:s', time()),
							'modifyer_id' => $this->view->user['id'],
							'modifyer_ip' => $_SERVER['REMOTE_ADDR']
							);
			if(isset($password_modifyed) and $password_modifyed) {
				$user['password'] = $aM->encrypt($datas['password']);
			}
			
			$result = $uM->set($this->view->user['id'], $user);
			
			if($result) {
			
				// Update user's view information
				$user = $uM->updateInformations($this->view->user['id']);
				
				exit(Zend_Json::encode(array('result' => true, 'message' => 'Modifications effectuées')));
			
			} else {
				exit(Zend_Json::encode(array('result' => false, 'message' => 'Aucune modification effectuée')));
			}
		
		}
	}
?>
